﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Ninject;
using SydEnergiPrototype.DataAccess;
using SydEnergiPrototype.Logic;
using SydEnergiPrototype.Model;

namespace SydEnergiPrototype.ViewModel
{
    public class FinalizeEditViewModel : ViewModelBase, IFinalizeEditViewModel
    {
        private RelayCommand _saveEntry;
        public RelayCommand SaveEntry
        {
            get
            {
                if(_saveEntry == null)
                    _saveEntry = new RelayCommand(param => Save());

                return _saveEntry;
            }
        }

        private string _userName;
        public string UserName 
        { 
            get { return _userName; }
            set { _userName = value; Notify("UserName"); }
        }

        public DateTime Date
        {
            get { return DateTime.Now; }
        }

        private string _comment;
        public string Comment
        {
            get { return _comment; }
            set
            {
                if(!string.IsNullOrWhiteSpace(value))
                    _comment = value; Notify("Comment");
            }
        }

        public string SelectedReason { get; set; }

        private List<string> _reasons; 
        public List<string> Reasons 
        { 
            get
            {
                if(_reasons == null)
                {
                    _reasons = new List<string>();
                    _reasons.Add("Oprettet");
                    _reasons.Add("Registreret forkert");
                    _reasons.Add("Knækket");
                }
                return _reasons;
            } 
        }

        private readonly IDataService _dataService;
        private readonly IMemoryDataStore _memoryDataStore;

        [Inject]
        public FinalizeEditViewModel(IDataService dataService, IMemoryDataStore memoryDataStore)
        {
            if(dataService == null)
                throw new ArgumentNullException("dataService");
            _dataService = dataService;

            if(memoryDataStore == null)
                throw new ArgumentNullException("memoryDataStore");
            _memoryDataStore = memoryDataStore;
        }

        private void Save()
        {
            if (!string.IsNullOrWhiteSpace(UserName))
            {
                var entry = _memoryDataStore.RetrieveEntry();

                var historyEntry = !string.IsNullOrWhiteSpace(Comment) ? new HistoryEntry(Date, UserName, SelectedReason, Comment) : new HistoryEntry(Date, UserName, SelectedReason);

                entry.EntryHistory.Add(historyEntry);
                _dataService.PersistEntry(new ReturnResult(null, entry));
            }
        }
    }
}
